//
//  MapViewController.m
//  LiangYa_BObject
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 YuHao. All rights reserved.
//

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "KCAnnotation.h"

@interface MapViewController ()<MKMapViewDelegate, CLLocationManagerDelegate>
@property (nonatomic, strong) MKMapView *map;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) UIButton *backB;
@property (nonatomic, strong) NSArray *arr;
@property (nonatomic, assign) BOOL flag;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.flag = YES;
    NSString *str = self.ybj_position;
    self.arr = [str componentsSeparatedByString:@","];
    
    // Do any additional setup after loading the view.
    self.locationManager = [[CLLocationManager alloc] init];
    
    //设置代理人
    self.locationManager.delegate = self;
    
    // 设置精确度
    self.locationManager.desiredAccuracy =kCLLocationAccuracyBest;
    
    // 使用的时候授权(手动开)
    // [self.locationManager requestWhenInUseAuthorization];
    
    
    //    请求获得应用使用时的定位服务授权，注意使用此方法前在要在info.plist中配置NSLocationWhenInUseUsageDescription
    
    
    [self.locationManager requestAlwaysAuthorization];
    
//        请求获得应用一直使用定位服务授权，注意使用此方法前要在info.plist中配置NSLocationAlwaysUsageDescription
//    
//    
//    开始定位
//        [self.locationManager startUpdatingLocation];
    
        self.map = [[MKMapView alloc] initWithFrame:self.view.bounds];
        self.map.userTrackingMode = MKUserTrackingModeFollow;
        // 显示地图位置
        self.map.showsUserLocation = YES;
        // 地图类型
        self.map.mapType = MKMapTypeStandard;
        self.map.showsBuildings =YES;// 显示建筑!!
        self.map.showsPointsOfInterest =YES;
        self.map.delegate = self;
    
        [self.view addSubview:self.map];
    
    
    
    // 大头针坐标
    
    CGFloat a = [[self.arr firstObject] floatValue];
    CGFloat b = [[self.arr lastObject] floatValue];
    
    
    CLLocation *locLiangpi = [[CLLocation alloc] initWithLatitude:b longitude:a];
    [self addAnnotation:locLiangpi shopName:self.suit_address];
    
//    [self.view addSubview:locLiangpi];
    
    self.backB = [UIButton buttonWithType:UIButtonTypeSystem];
    _backB.backgroundColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.139 alpha:1.000];
    _backB.frame = CGRectMake(20, 20, 40, 40);
    _backB.layer.cornerRadius = 20;
    UIImage *backI = [[UIImage imageNamed:@"picture1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [_backB setImage:backI forState:UIControlStateNormal];
    [_backB setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_backB addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backB];
    
}

- (void)backAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}




#pragma mark - MKMapViewDelegate 去往定位的那个位置

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       
   

  
    MKCoordinateSpan span = MKCoordinateSpanMake(0.000021, 0.000066);//这个显示大小精度自己调整
    
    //    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    
    CGFloat a = [[self.arr firstObject] floatValue];
    CGFloat b = [[self.arr lastObject] floatValue];

    CLLocation *locLiangpi = [[CLLocation alloc] initWithLatitude:b  longitude:a];
    MKCoordinateRegion region = MKCoordinateRegionMake(locLiangpi.coordinate, span);
    
    [mapView setRegion:region animated:YES];
   
     });
   
    
    [self.locationManager stopUpdatingLocation];
}




//设置大头针

-(void)addAnnotation:(CLLocation *)location shopName:(NSString *)shopName
{
    
//        CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(38.896519, 121.537317);
    
    KCAnnotation *annotation1=[[KCAnnotation alloc]init];
    annotation1.title=shopName;
    annotation1.coordinate=location.coordinate;
    [self.map addAnnotation:annotation1];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
